package com.vincent.suchao.lcld;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.util.EncodingUtils;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

import com.vincent.suchao.entity.ZXDT_NewsFeed_Detail;
import com.vincent.suchao.util.Util;

public class ZXDT_DetailActivity extends Activity implements OnClickListener {
	private TextView tv_tittle;
	private Button b_back;
	private String url;
	private static final int MSG_LOAD_SUCCESS = 1;
	private static final int MSG_LOAD_FAIL = MSG_LOAD_SUCCESS + 1;
	private Dialog loadingBar;
	private WebView wv_detail;
	ZXDT_NewsFeed_Detail zxdt;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_LOAD_SUCCESS:
				if (loadingBar != null)
					loadingBar.dismiss();
				String content = getContentHtml(zxdt);

				wv_detail.loadDataWithBaseURL(null, content, "text/html", "utf-8", null);
				wv_detail.setWebViewClient(new WebViewClient() {
					@Override
					public void onPageFinished(WebView view, String url) {
 
						String u = "javascript:setAllImagesWidth('300');";
						wv_detail.loadUrl(u);

					}

				});

				break;
			case MSG_LOAD_FAIL:
				if (loadingBar != null)
					loadingBar.dismiss();
				break;
			}
		}
	};

	private String getContentHtml(ZXDT_NewsFeed_Detail zxdt) {
		InputStream in = null;
		String body = null;

		StringBuffer res = new StringBuffer();
		try {
			in = getResources().openRawResource(R.raw.test2);

			InputStreamReader inputreader = new InputStreamReader(in, "UTF-8");
			BufferedReader buffreader = new BufferedReader(inputreader);
			String line;
			while ((line = buffreader.readLine()) != null) {

				res.append(line);
			}
			body = res.toString();
			body = body.replace("%@tittle", zxdt.getTitle());
			body = body.replace("%@body", zxdt.getContent());

			body = EncodingUtils.getString(body.getBytes(), "UTF-8");
			System.out.println("body " + body);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return body;
	}

	private int id = 0;

	// "android.resource://com.vincent.suchao.lcld/" + R.raw.tab1_video
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zxdt_detail);
		init();
		id = getIntent().getIntExtra("ZXDT_NewsFeed", 0);
		getNews();
	}

	private void getNews() {
		loadingBar.show();
		new Thread(new Runnable() {

			@Override
			public void run() {
				String json = Util.testCGAL_Detail(id);
				// System.out.println("json: " + json);
				try {
					String result = Util.post(Util.NEWS_URL2, json);
					System.out.println("aa result "+result);
					
					
					zxdt = Util.parseZXDTJSONDetail(result);
					Message msg = mHandler.obtainMessage();
					msg.what = MSG_LOAD_SUCCESS;
					mHandler.sendMessage(msg);
				} catch (Exception e) {
					e.printStackTrace();
					Message msg = mHandler.obtainMessage();
					msg.obj = e.getMessage();
					msg.what = MSG_LOAD_FAIL;
					mHandler.sendMessage(msg);
				}
			}
		}).start();
	}

	private void init() {
		loadingBar = Util.progressDialog(this, R.string.loading, R.string.loading, false);
		tv_tittle = (TextView) findViewById(R.id.tv_tittle);
		tv_tittle.setText(getString(R.string.menu_com_zxdt));
		b_back = (Button) findViewById(R.id.b_back);
		b_back.setOnClickListener(this);
		wv_detail = (WebView) findViewById(R.id.wv_detail);
		wv_detail.getSettings().setJavaScriptEnabled(true);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.gsjj, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.b_back:
			finish();
			break;
		default:
			break;
		}

	}

}
